﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class RetrieveRuleChecklistByIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRuleChecklistByIdRequest, RetrieveRuleChecklistByIdResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        public int GroupId;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveRuleChecklistByIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRuleChecklistByIdRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            GroupId = request.RuleId;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveRuleChecklistByIdResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ChecklistRulesRecord> records = new List<ChecklistRulesRecord>();

                List<ChecklistQuestionSubProductResult> listCustomer = new List<ChecklistQuestionSubProductResult>();
                listCustomer = Providers.ProductHandbooks.RetrieveListRuleChecklistById(out totalRecord, GroupId, Dao, TextSearch, Request.Lang);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new ChecklistRulesRecord
                    {
                        SubProductId = x.SubProductId,
                        AnswerGroup = x.AnswerGroup,
                        QuestionId = x.QuestionId,
                        QuestionAnswer = x.Answer,
                        RuleId = x.QSpResultId
                    }).ToList();
                }

                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
